package com.example.mydianping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.mydianping.dto.Result;
import com.example.mydianping.dto.UserDTO;
import com.example.mydianping.mapper.FollowMapper;
import com.example.mydianping.mapper.UserMapper;
import com.example.mydianping.pojo.Follow;
import com.example.mydianping.pojo.User;
import com.example.mydianping.service.FollowService;
import com.example.mydianping.utils.UserHolder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class FollowServiceImpl implements FollowService {

    @Resource
    private FollowMapper followMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UserMapper userMapper;

    @Override
    public Result isFollow(Long id) {
        //判断当前登录用户是否关注该博主
        UserDTO user = UserHolder.getUser();
        if(user == null){
            return Result.fail("未登录，请先完成登录");
        }
        Integer count = followMapper.judgeFollow(id, user.getId());
        if(count != null && count > 0){
            //已关注
            return Result.ok(true);
        }else {
            //未关注
            return Result.ok(false);
        }
    }

    @Override
    public Result follow(Long id, Boolean isFollow) {
        if(isFollow == null){
            return Result.fail("空指针异常");
        }
        UserDTO user = UserHolder.getUser();
        if(user == null){
            return Result.fail("未登录，请先完成登录");
        }
        String followKey = "user:follows:"+user.getId();
        if(isFollow){
            //关注用户
            Follow follow = new Follow();
            follow.setUserId(id);
            follow.setFollowUserId(user.getId());
            followMapper.insert(follow);
            //TODO 将当前博主挂在该用户的关注用户下
            stringRedisTemplate.opsForSet().add(followKey,Long.toString(id));
            return Result.ok(follow);
        }else {
            //取关用户
            followMapper.deleteFan(id,user.getId());
            //TODO 将当前博主从该用户的关注列表中移除
            stringRedisTemplate.opsForSet().remove(followKey,Long.toString(id));
            return Result.ok("delete:"+user.getId()+" from "+id);
        }
    }

    @Override
    public Result getSame(Long id) {
        UserDTO user = UserHolder.getUser();
        if(user == null){
            return Result.fail("未登录，请先完成登录");
        }
        String followKey1 = "user:follows:"+user.getId();
        String followKey2 = "user:follows:"+id;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(followKey1, followKey2);
        //将所有 userId 下的用户封装成 UserDTO 后以集合的形式返回
        List<UserDTO> users = new ArrayList<>();
        if(intersect == null){//没有共同关注用户，直接返回空集合
            return Result.ok(users);
        }
        for(String stringUserId : intersect){
            Long userId = Long.parseLong(stringUserId);
            User user1 = userMapper.selectById(userId);
            UserDTO userDTO = BeanUtil.copyProperties(user1,UserDTO.class);
            users.add(userDTO);
        }
        return Result.ok(users);
    }
}
